package com.netty.hanler;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LoggingHandler;

import java.util.Arrays;

/**
 * 日志信息
 *
 * @author L
 * @since 2024-
 */
public class ServerChannelHandler extends ChannelInitializer<NioSocketChannel> {

    public static final LoggingHandler LOGGING_HANDLER = new LoggingHandler();
    private final ChannelHandler[] handlers;

    public ServerChannelHandler(ChannelHandler... handlers) {
        this.handlers = handlers;
    }

    @Override
    protected void initChannel(NioSocketChannel serverChannel) throws Exception {
        final ChannelPipeline pipeline = serverChannel.pipeline();
        // 添加日志信息
        pipeline.addLast(LOGGING_HANDLER);

        // 扩展 handler
        if (handlers != null && handlers.length > 0) {
            Arrays.stream(handlers).forEach(pipeline::addLast);
        }
    }
}
